# -*- coding: utf-8 -*-
import scrapy
from Aoisolas.items import AoisolasItem

class AoisolaspiderSpider(scrapy.Spider):
    name = "Aoisola"
    allowed_domains = ["www.meituba.com"]
    # allowed_domains = ["http://www.meituba.com/xinggan/"]
    start_urls = ['http://www.meituba.com/tag/liufeier.html']

    def parse(self, response):
        # list = response.css(".list-left dd:not(.page)")
        list = response.css(".mainbody2_st .showhidepic11")

        for img in list:
            # imgname = img.css("a::text").extract_first()
            # imgurl = img.css("a::attr(href)").extract_first()
            imgname = img.css("a::attr(title)").extract_first()
            imgurl = img.css("a::attr(href)").extract_first()
            imgurl2 = str(imgurl)
            print("ppppppppppppppppppppppppppppppppppppppppp")
            print(imgurl2)
            # next_url = response.css(".page-en:nth-last-child(2)::attr(href)").extract_first()
            # if next_url is not None:
            #     # 下一页
            #     yield response.follow(next_url, callback=self.parse)

            # yield scrapy.Request(imgurl2, callback=self.content,dont_filter=True)
            yield scrapy.Request(imgurl2, callback=self.content)

    def content(self, response):
        item = AoisolasItem()
        # item['name'] = response.css(".content h5::text").extract_first()
        # item['ImgUrl'] = response.css(".content-pic img::attr(src)").extract()
        item['name'] = response.css(".tit_top h1::text").extract_first()
        item['ImgUrl'] = response.css(".photo img::attr(src)").extract()
        yield item
        # 提取图片,存入文件夹
        # print(item['ImgUrl'])
        # next_url = response.css(".page-ch:last-child::attr(href)").extract_first()
        next_url = response.css(".pages ul li:last-child a::attr(href)").extract_first()

        if next_url is not None:
            # 下一页
            yield response.follow(next_url, callback=self.content)
